﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using DbHelper;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace bugAlarm {
    public partial class FormDataMonitor:Form {
        private MySqlConnection conn;

        private Icon blank = Properties.Resources.blank;
        private Icon warnIcon = Properties.Resources.ooopic_1500567472;
        private Icon normalIcon = Properties.Resources.ooopic_1500566914;
        private bool _status = true;
        private bool _isBlink = false;

        private string flog = string.Empty;
        private string count = string.Empty;
        private string sql;
        public FormDataMonitor() {
            InitializeComponent();

            //设置自动换行
            this.dgv_tableInfo.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            //设置自动调整高度
            this.dgv_tableInfo.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
            this.dgv_tableInfo.AllowUserToAddRows = false;
            notifyIcon1.Icon = normalIcon;
            tb_dbName.Text = "log4net";
            tb_tableName.Text = "t_esiaaloginfo";
            tb_User.Text = "root";
            tb_Pwd.Text = "123456";
            tb_Host.Text = "localhost";
        }

        private void button1_Click(object sender,EventArgs e) {
            conn = new MySqlConnection(string.Format("Server={0};Database={1}; User={2};Password={3};",tb_Host.Text.Trim(),tb_dbName.Text.Trim(),tb_User.Text.Trim(),tb_Pwd.Text.Trim()));
            sql = string.Format("select count(*) from {0}",tb_tableName.Text.Trim());
            count = MySqlTool.ExecuteScalar(conn,CommandType.Text,sql).ToString();
            flog = count;
            conn.Close();

            dbTime.Enabled = true;
            dbTime.Start();

            tb_dbName.Enabled = false;
            tb_tableName.Enabled = false;
            tb_User.Enabled = false;
            tb_Pwd.Enabled = false;
            tb_Host.Enabled = false;
            btn_Start.Enabled = false;
            this.Hide();
        }

        private void 显示ToolStripMenuItem_Click(object sender,EventArgs e) {
            this.Show();
        }

        private void 隐藏ToolStripMenuItem_Click(object sender,EventArgs e) {
            this.Hide();
        }

        private void 退出ToolStripMenuItem_Click(object sender,EventArgs e) {
            Environment.Exit(0);
        }

        /// <summary>
        /// 数据库监测定时器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DBTimer_Tick(object sender,EventArgs e) {
            count = MySqlTool.ExecuteScalar(conn,CommandType.Text,sql).ToString();

            bindingSource1.DataSource = MySqlTool.ExecuteDataTable(conn,CommandType.Text,string.Format("select * from {0} order by id desc",tb_tableName.Text.Trim()));
            this.dgv_tableInfo.DataSource = bindingSource1;

            conn.Close();
            if(!count.Equals(flog)) {
                if(_isBlink == false) {
                    notifyIcon1.ShowBalloonTip(5000,"提示","异常待处理！",ToolTipIcon.Warning);
                    _isBlink = true;

                    warnTime.Enabled = true;
                    warnTime.Start();
                }
            }
        }

        /// <summary>
        /// 报警定时器事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void warnTimer_Tick(object sender,EventArgs e) {
            if(_status) {
                notifyIcon1.Icon = warnIcon;
            } else {
                notifyIcon1.Icon = blank;
            }
            _status = !_status;
        }

        /// <summary>
        /// 关闭按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormDataMonitor_FormClosing(object sender,FormClosingEventArgs e) {
            if(e.CloseReason == CloseReason.UserClosing) {
                e.Cancel = true;    //取消"关闭窗口"事件
                this.Hide();
            }
        }
    }
}
